package com.zmj.controller;

import com.zmj.dto.UserDTO;
import com.zmj.entity.User;
import com.zmj.service.impl.UserServiceImpl;
import com.zmj.utils.Log;
import com.zmj.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.UUID;

@Controller
public class UserController {

    @Autowired
    UserServiceImpl userService;
    /*@Autowired
    private Producer producer;
*/


    @RequestMapping("/toRegister")
    public String toRegister(){
        return "register";
    }

    @RequestMapping("/register")
    public String register( MultipartFile image, UserDTO userDTO, Map<String,Object> map, HttpSession session) throws IOException {
        String  c=userDTO.getCaptcha();
        if (c==null){
            map.put("message","验证码不能为空");
            return "register";
        }
        String  b=(String) session.getAttribute("code");
        if (!c.equals(b)){
            map.put("message","验证码不一致");
            return "register";
        }
        if (!userDTO.getPassword().equals(userDTO.getNextPassword())){
            map.put("message","两次密码不一致");
            return "register";
        }

            InputStream is=image.getInputStream();
            String path=session.getServletContext().getRealPath("img");
            File file=new File(path);
            if (!file.exists()){
                file.mkdir();
            }
            String fileName=image.getOriginalFilename();
            String suffix=fileName.substring(fileName.lastIndexOf("."));
            String newName= UUID.randomUUID().toString().replace("-","")+suffix;
            FileOutputStream fos=new FileOutputStream(path+File.separator+newName);
            FileCopyUtils.copy(is,fos);

            String savePath="img"+File.separator+newName;
            userDTO.setImg(savePath);

            boolean flg= userService.register(userDTO);
            if (flg){
                map.put("user",userDTO);
                return "index";
            }
            return "register";


    }

    @RequestMapping("/checkName")
    @ResponseBody
    public Message checkName(String userName){
        Message message=new Message();
        User user=userService.checkName(userName);
        if (user==null){
            message.setCode(1);
            message.setMes("此用户名可用");
        }else {
            message.setCode(0);
            message.setMes("此用户名已注册");
        }
        return message;
    }
  /*  @RequestMapping("/code")
    public void  getCode(HttpServletResponse response, HttpSession session){
        try {
            String code = producer.createText();
            System.out.println("code:" + code);
            session.setAttribute("code", code);

            BufferedImage bufferedImage = producer.createImage(code);

            ImageIO.write(bufferedImage, "jpg", response.getOutputStream());

        }catch (Exception e){
            e.printStackTrace();
        }
    }*/

  @RequestMapping("/aop")
  @Log
    public String haha(){
      System.out.println("一些方法");
      return "index";
  }

}
